package com.thero.core.net;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created by wusk on 2017/11/28.
 * 消息协议编码器
 */
public class StreamEncoder  extends MessageToByteEncoder<ProtocolMessage> {

    @Override
    protected void encode(ChannelHandlerContext context, ProtocolMessage message, ByteBuf buffer) throws Exception {
        MessageHeader header = message.getHeader();
        buffer.writeInt(header.getLength());
        buffer.writeShort(header.getCount());
        buffer.writeShort(header.getIndex());
        buffer.writeShort(header.getCommand());
        buffer.writeBytes(message.getMessage());
    }
}
